package com.shangxia.dreaming.business.pojo;

import com.shangxia.dreaming.common.content.ConditionContent;
import com.shangxia.dreaming.common.util.txtUtil.ReadTXT;
import com.shangxia.dreaming.common.util.txtUtil.WriteTXT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

/**
 * @ProjectName: dreaming
 * @ClassName: SixJob
 * @Time: 2020/10/30
 * @Author: zhouy
 * @Description: 简单取号  移除或保留某些号码
 * 0-3次错误号
 * @Experience:
 */

@Slf4j
@Component
public class Sevenob {

    public void work() throws IOException {
        List<List<Integer>> removeCodeLists = ReadTXT.readList_List_int("移除号码");

        List<List<Integer>> saveCodeLists = ReadTXT.readList_List_int("保留号码");

        Set<List<Integer>> list = getSets(removeCodeLists, saveCodeLists);

        if (list.size() > 0) {
            WriteTXT.saveLists(list, "全结果");
        }
    }

    private Set<List<Integer>> getSets(List<List<Integer>> removeCodeLists, List<List<Integer>> saveCodeLists) {
        String[] split = ConditionContent.TYPE2.split("-");

        Set<List<Integer>> list = creatList(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        Iterator<List<Integer>> iterator = list.iterator();
        BIG_WHILE:
        while (iterator.hasNext()) {
            List<Integer> next = iterator.next();
            if (removeCodeLists.size() > 0) {
                for (List<Integer> removeCodeList : removeCodeLists) {
                    if (next.containsAll(removeCodeList)) {
                        iterator.remove();
                        continue BIG_WHILE;
                    }
                }
            }
            if (saveCodeLists.size() > 0) {
                for (List<Integer> saveCodeList : saveCodeLists) {
                    if (!next.containsAll(saveCodeList)) {
                        iterator.remove();
                        continue BIG_WHILE;
                    }
                }
            }
        }
        return list;
    }

    private Set<List<Integer>> saveSets(List<List<Integer>> saveCodeLists) {
        String[] split = ConditionContent.TYPE2.split("-");

        Set<List<Integer>> list = creatList(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        Iterator<List<Integer>> iterator = list.iterator();
        BIG_WHILE:
        while (iterator.hasNext()) {
            List<Integer> next = iterator.next();
            if (saveCodeLists.size() > 0) {
                for (List<Integer> saveCodeList : saveCodeLists) {
                    if (!next.containsAll(saveCodeList)) {
                        iterator.remove();
                        continue BIG_WHILE;
                    }
                }
            }
        }
        return list;
    }

    private Set<List<Integer>> removeSets(List<List<Integer>> removeCodeLists) {
        String[] split = ConditionContent.TYPE2.split("-");
        Set<List<Integer>> list = creatList(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
        Iterator<List<Integer>> iterator = list.iterator();
        BIG_WHILE:
        while (iterator.hasNext()) {
            List<Integer> next = iterator.next();
            if (removeCodeLists.size() > 0) {
                for (List<Integer> removeCodeList : removeCodeLists) {
                    if (next.containsAll(removeCodeList)) {
                        iterator.remove();
                        continue BIG_WHILE;
                    }
                }
            }
        }
        return list;
    }

    private Set<List<Integer>> creatList(int maxSum, int sum) {
        Set<List<Integer>> sets = new LinkedHashSet<>();
        for (int i = 1; i < maxSum + 2 - sum; i++) {
            for (int j = i + 1; j < maxSum + 3 - sum; j++) {
                if (sum == 2) {
                    List<Integer> list = new ArrayList<>();
                    list.add(i);
                    list.add(j);
                    sets.add(list);
                }
                for (int k = j + 1; k < maxSum + 4 - sum; k++) {
                    if (sum == 3) {
                        List<Integer> list = new ArrayList<>();
                        list.add(i);
                        list.add(j);
                        list.add(k);
                        sets.add(list);
                    }
                    for (int m = k + 1; m < maxSum + 5 - sum; m++) {
                        if (sum == 4) {
                            List<Integer> list = new ArrayList<>();
                            list.add(i);
                            list.add(j);
                            list.add(k);
                            list.add(m);
                            sets.add(list);
                        }
                        for (int n = m + 1; n < maxSum + 6 - sum; n++) {
                            if (sum == 5) {
                                List<Integer> list = new ArrayList<>();
                                list.add(i);
                                list.add(j);
                                list.add(k);
                                list.add(m);
                                list.add(n);
                                sets.add(list);
                            }
                            for (int s = n + 1; s < maxSum + 7 - sum; s++) {
                                if (sum == 6) {
                                    List<Integer> list = new ArrayList<>();
                                    list.add(i);
                                    list.add(j);
                                    list.add(k);
                                    list.add(m);
                                    list.add(n);
                                    list.add(s);
                                    sets.add(list);
                                }
                            }
                        }
                    }
                }
            }
        }
        return sets;
    }
}
